/* 
Zabijaka Map Management

Plugin for choosing nextmap and map time.
With this plugin you can dissable plugins:
nextmap.amxx
mapchooser.amxx
timeleft.amxx
any other plugins for map vote, map time vote, rockthevote

Plugin allows:
	- voting for nextmap
	- specifying win type for voting for nextmap (map with most votes, map with some percent of votes)
	- voting for map time 
	- specifying win type for voting for map time (time with most votes, time with some percent of votes, average - sum of selected times / votes count)
	- using rtv to force voting for nextmap
	- checking minimum/maximum players count for specified map when nominating/selecting random nextmap
	- checking if map was played lately when nominating/selecting random renxtmap
	- displaying :
		current map name
		next map name (if choosen)
		timeleft (with "say timeleft" command or constant hud message)
		nominations (with "say noms" / "say nominations" command or constant hud message)
		current time
		friendly fire status
		all maps with min/max players settings
		recently played maps
	- managing maps from admin menu
	
Maps settings are read only once. Than they are stored in vault file.
If you want to add/remove or modify map you can do it in game from admin menu or modify ini file.
After modifying through menu, settings will be saved only in vault file, unless you choose to save settings from menu.
After saving settings from menu, ini file will be overriten with current maps an their settings.
If you modify ini file by hand you have to use mm_refreshmaps command to read new settings.


#################################################################
#################################################################
###							      ###
###			COMMANDS			      ###
###							      ###
#################################################################
#################################################################


*****************************************************************
*								*
*		Global Commands					*
*								*
*****************************************************************
	
	- say thetime 	
			- Displays current time. (depends on cvar mm_use_thetime)
	- say timeleft 	
			- Displays time to map ends. (depends on cvar mm_use_timeleft)
	- say nextmap 	
			- Displays next map if it has been chosen. (depends on cvar mm_use_nextmap)
	- say currentmap 
			- Displays current map name. (depends on cvar mm_use_currentmap)
	- say ff	
			- Displays friendly fire status. (depends on cvar mm_use_ff)
	
*****************************************************************
*								*
*		Map Vote Commands				*
*								*
*****************************************************************
	- say listmaps	
			- Displays available maps with min/max players settings. Maps are listed in console.
	- listmaps 	
			- (console command) The same as above
	
	- say recent 	
			- Displays list of recently played maps. Maps are listed in console
	- recent	
			- (console command) The same as above
	- say recentmaps 
			- The same as above
	- recentpams	
			- (console command) The same as above
	

*****************************************************************
*								*
*		Map Nominate Commands				*
*								*
*****************************************************************
	- say noms	
			- Displays nominated maps
	- noms		
			- (console command) The same as above
	- say nominations 
			- The same as above
	- nominations	
			- (console command) The same as above
	- say [MAP_NAME]
			- Nominates a map. 
			e.g.
			say de_dust - will nominate map de_dust
			If map is not found will try to find map with each of this prefixes:
			de_, cs_, as_, fy_, he_, aim_, awp_ , fun_, as_

			
*****************************************************************
*								*
*		Rockthevote Commands				*
*								*
*****************************************************************
	- say rtv	
			- Gives a vote for map change 
	- say rockthevote
			- The same as above
	- amx_rtv
			- (console command) (ADMIN_BAN flag required) Forces map change
	
	
*****************************************************************
*								*
*		Map Management Commands				*
*								*
*****************************************************************
	- mm_refreshmaps
			- (console command) (ADMIN_CVAR flag required)
			Refreshes all maps (will delete each map from vault file, and read config/mm_maps.ini
	- mm_mapsmenu
			- (console command) (ADMIN_CVAR flag required)
			Opens menu for maps managing. Available options:
				- Add Map
				- Delete Map
				- Edit Map
				- Save Settings
	
			


#################################################################
#################################################################
###							      ###
###			CVARS				      ###
###							      ###
#################################################################
#################################################################


*****************************************************************
*								*
*		Global CVAR's					*
*								*
*****************************************************************


	- mm_active 	(1)
		 1/0 - 	Turns plugin ON/OFF
	- mm_info	(1)
		 1/0 - 	Turns displaying info about available commands ON/OFF
	- mm_info_delay	(30)
		-	Delay in seconds between infos
	- mm_use_thetime (1)
		1/0 - 	Saying thetime will show current time
	- mm_use_timeleft (1)
		1/0 - 	Saying timeleft will show time left for current map
	- mm_use_nextmap (1)
		1/0 - 	Saying nextmap will show next map if it choosen
	- mm_use_currentmap (1)
		1/0 - 	Saying currentmap will show currentmap name
	- mm_use_ff	(1)
		1/0 - 	Saying ff will show friendly fire status

	- mm_timevoice	(1)
		1/0 - 	Turns voice when saying thetime/timeleft ON/OFF

	- mm_show_timeleft (1)
		1/0 -	Shows/hides timeleft clock



*****************************************************************
*								*
*		Map Vote CVAR's					*
*								*
*****************************************************************

	- mm_map_vote_win	(1)
		-	Win type
		0 - 	Wins map with most votes.
		1 - 	Wins map with most votes. Map has to have x percent of all votes, where x is mm_map_vote_win_percent.
			If map does not have required percent of votes, second vote with 2 maps with most votes will be shown.
			In second vote map with most votes wins.
	- mm_map_vote_win_percent (50)
		-	How many percent of votes map has to have to win if mm_map_vote_win = 1
	- mm_map_vote_delay 	(15)
		-	Delay in seconds from round start, after whitch vote for next map will be displayed.
	- mm_map_vote_time 	(20)
		- 	How many seconds to give vote
	- mm_maps	(5)
		-	Number of maps for vote (max 5 defined with MAX_MAPS)
	- mm_random_check_min	(1)
		1/0 -	Turns checking minimum required players for map, when getting random map, ON/OFF
	- mm_random_check_max	(1)
		1/0 -	Turns checking maximum allowed players for map, when getting random map, ON/OFF
	- mm_random_last_maps	(6)
		- 	If more than 0 skips last x maps played when getting random map. x is the number specified
			(max 10 defined with MAX_RECENT_MAPS)
	- mm_map_results	(1)
		-	Displays result of voting in real time (HUD)
		0 -	Do not display
		1 -	Display all results
		2 - 	Display top 2 maps

		
*****************************************************************
*								*
*		Map Time Vote CVAR's				*
*								*
*****************************************************************

	- mm_time_vote	(1)
		1/0	- Turns voting for map time ON/OFF
	- mm_time_vote_round (3)
		-	Rond at which vote will be displayed.
	- mm_times	(10 15 20 25 30)
		- 	Times to vote for (separated with coma, max 5 defined with MAX_MAP_TIMES)
	- mm_time_vote_delay	(15)
		-	Delay in seconds from round start, after whitch vote for map time will be displayed.
	- mm_time_vote_time 	(20)
		- 	How many seconds to give vote
	- mm_time_vote_win	(0)
		-	Win type
		0 - 	Wins time with most votes.
		1 - 	Wins time with most votes. Time has to have x percent of all votes, where x is mm_time_vote_win_percent.
			If time does not have required percent of votes, second vote with 2 times with most votes will be shown.
			In second vote time with most votes wins.
		2 -	Map time will be: sum of all selected times / votes count
	- mm_time_vote_win_percent (50)
		- 	How many percent of votes time has to have to win if mm_time_vote_win = 1
	- mm_time_vote_results	(1)
		-	Displays result of voting in real time (HUD)
		0 -	Do not display
		1 -	Display all results
		2 - 	Display top 2 times
		3 - 	Displays counted time (only for mm_time_vote_win = 2)
	

*****************************************************************
*								*
*		Map Nominate CVAR's				*
*								*
*****************************************************************
	
	- mm_nom	(1)
		1/0 - 	Turns nominations ON/OFF
	- mm_nom_max	(3)
		- 	Maximum number of nominations (not more than mm_maps)
	- mm_nom_check_min	(1)
		1/0 - 	Turns checking minimum required players for map, when nominatimg map, ON/OFF
	- mm_nom_check_max 	(1)
		1/0 - 	Turns checking maximum allowed players for map, when nominatimg map, ON/OFF
	- mm_nom_check_last	(6)
		- 	If more than 0 refuse to nominate map if it was one of last x. x is the number specified
			(max 10 defined with MAX_RECENT_MAPS)
	- mm_noms_show	(1)
		1/0 - 	Shows/hides displaying nominated maps
*****************************************************************
*								*
*		Rockthevote CVAR's				*
*								*
*****************************************************************
	
	- mm_rtv	(1)
		1/0 -	Allows/disallows to say rtv to force map change
	- mm_rtv_admin	(1)
		1/0 - 	Turns admin "amx_rtv" command ON/OFF
	- mm_rtv_delay	(300)
		-	Delay in seconds from map starts, after whitch players can use rtv
	- mm_rtv_percent (70)
		-	How many percent of players have tu use rtv to force map change
	

	
#################################################################
#################################################################
###							      ###
###			CONFIGURATION			      ###
###							      ###
#################################################################
#################################################################

	- Config file: 	configs/mm_config.cfg
	- Maps file: 	configs/mm_maps.ini
		- map file format:
			map_name min_players max_players
			0 in min_players or max_players meens no limitation
			e.g.
			de_dust 10 0
				- de_dust can be played/nominated if there is at least 10 players
			awp_map	0 10
				- awp_map can be played/nominated if there is not more than 10 players
			de_dust2 4 14
				- de_dust2 can be played/nominated if there is at least 4 and not more than 14 players
			aim_map 0 0 
				- aim_map can be played always


 */

#include "include/mm_globals.inl"
#include "include/mm_time_vote.inl"

#if !defined MM_MAP_VOTE_INCLUDED
#include "include/mm_map_vote.inl"
#endif

#if !defined MM_NOMS_INCLUDED
#include "include/mm_noms.inl"
#endif

#if !defined MM_RTV_INCLUDED
#include "include/mm_rtv.inl"
#endif

#include "include/mm_maps_manager.inl"

#define PLUGIN "Zabijaka Map Management"
#define VERSION "2.0"
#define AUTHOR "Zabijaka"

#define TASK_FAKE_ROUND_START 2000
#define TASK_SHOW_INFO 2033

public plugin_init() {
	register_plugin(PLUGIN, VERSION, AUTHOR);
	register_logevent("roundStart",2, "1=Round_Start");
	
	plugin_init_globals();
	plugin_init_timeVote();
	plugin_init_mapsManager();
	plugin_init_mapVote();
	plugin_init_noms();
	plugin_init_rtv();
	set_task(30.0, "showInfo", TASK_SHOW_INFO);
}

public showInfo(){
	if(!get_pcvar_num(gCvarActive) || !get_pcvar_num(gCvarInfo))
		return;
	new Float: next = get_pcvar_float(gCvarInfoDelay);
	new shown = 0;
	new allowrtv = get_pcvar_num(gCvarRtv);
	new allownoms = get_pcvar_num(gCvarNom);
	while(shown == 0){
		switch(random_num(1, 6)){
			case 1: {
				if(allowrtv){
					client_print(0, print_chat, "%L", LANG_PLAYER, "INFO_RTV");
					shown = 1;
				}
			}
			case 2:{
				if(allownoms){
					client_print(0, print_chat, "%L", LANG_PLAYER, "INFO_NOMINATE_MAP");
					shown = 1;
				}
			}
			case 3:{
				if(allownoms){
					client_print(0, print_chat, "%L", LANG_PLAYER, "INFO_NOMS");
					shown = 1;
				}
			}			
			case 4:{
				client_print(0, print_chat, "%L", LANG_PLAYER, "INFO_RECENTMAPS");
				shown = 1;
			}
			case 5:{
				
				client_print(0, print_chat, "%L", LANG_PLAYER, "INFO_LISTMAPS");
				shown = 1;
			}
		}
	}
	set_task(next, "showInfo", TASK_SHOW_INFO);
}




public plugin_end(){
	set_pcvar_float(gCvarMP_TIMELIMIT, gMP_TIMELIMIT);
	set_pcvar_num(gCvarMP_CHATTIME, gMP_CHATTIME);
	
	if(task_exists(TASK_FAKE_ROUND_START))
		remove_task(TASK_FAKE_ROUND_START);
	if(task_exists(TASK_SHOW_INFO))
		remove_task(TASK_SHOW_INFO);
	if(task_exists(TASK_SHOW_NOMS))
		remove_task(TASK_SHOW_NOMS);
	if(task_exists(TASK_TIMELEFT_CLOCK))
		remove_task(TASK_TIMELEFT_CLOCK);
		
	new mapName[MAP_NAME_LEN];
	get_mapname(mapName, MAP_NAME_LEN -1);
	new key[16];
	new map[MAP_NAME_LEN];
	new timestamp;
	for(new i=MAX_RECENT_MAPS;i>0;i--) {
		formatex(key, 15, "%s%d", keyLastPlayed, i-1);
		if(nvault_lookup(gVault, key, map, MAP_NAME_LEN -1, timestamp)) {
			formatex(key, 15, "%s%d", keyLastPlayed, i);
			nvault_set(gVault,key, map);
		}
	}
	formatex(key, 15, "%s%d", keyLastPlayed, 0);
	nvault_set(gVault, key, mapName);
	
	nvault_close(gVault);
}

public plugin_cfg(){
	plugin_cfg_globals();
}
public roundStart(){
	if(task_exists(TASK_FAKE_ROUND_START))
		remove_task(TASK_FAKE_ROUND_START);
	roundStart_globals();
	roundStart_timeVote();
	roundStart_mapVote();
	roundStart_noms();
	checkMapChange();
	set_task(getRoundTimeSeconds() - getRoundSeconds() + 1.0, "roundStart", TASK_FAKE_ROUND_START);
}

public client_connect(id){
	client_connect_timeVote(id);
	client_connect_mapVote(id);
	client_connect_noms(id);
	client_connect_rtv(id);
}
public client_disconnect(id){
	client_disconnect_timeVote(id);
	client_disconnect_mapVote(id);
	client_disconnect_noms(id);
	client_disconnect_rtv(id);
}

